//
//  cCContactsViewController.m
//  cyChat
//
//  Created by 楼楚 on 15/12/9.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "cCContactsViewController.h"
#import "cCChatViewController.h"
@interface cCContactsViewController ()<NSFetchedResultsControllerDelegate>{
    NSFetchedResultsController *_resultsControl;
}

@property (nonatomic ,strong) NSArray *friends;

@end

@implementation cCContactsViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
//
    [self loadFriends2];
    
    
}

//- (void) loadFriends
//{
//    
//    //    如何使用coredata获取数据
//    
//    //    1.上下文【关联到数据】
//   NSManagedObjectContext *context = [cCXMPPTool sharedcCXMPPTool].rosterStorge.mainThreadManagedObjectContext;
//    
//    //    2.fetchrequest
//    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
//    
//    //    3.设置过滤和排序
////    过滤当前登录用户的好友
//    NSString *jid = [cCUserInfo sharedcCUserInfo].jid;
//    
//    NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@",jid];
//    request.predicate = pre;
////    排序
//    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
//    request.sortDescriptors = @[sort];
//    
//    //    4.执行请求获取数据
//    self.friends = [context executeFetchRequest:request error:nil];
//    NSLog(@"%@",self.friends);
//
//}
//
- (void) loadFriends2
{
    
    //    如何使用coredata获取数据
    
    //    1.上下文【关联到数据】
    NSManagedObjectContext *context = [cCXMPPTool sharedcCXMPPTool].rosterStorge.mainThreadManagedObjectContext;
    
    //    2.fetchrequest
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
    
    //    3.设置过滤和排序
    //    过滤当前登录用户的好友
    NSString *jid = [cCUserInfo sharedcCUserInfo].jid;
    
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@",jid];
    request.predicate = pre;
    //    排序
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
    request.sortDescriptors = @[sort];
    
    //    4.执行请求获取数据
    _resultsControl = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
    _resultsControl.delegate = self;
    NSError *err = nil;
    
    [_resultsControl performFetch:&err];
    if (err) {
        NSLog(@"%@",err);
    }
    
}

#pragma mark - 数据内容改变时调用的方法
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    NSLog(@"changeeeee");
//    刷新表格
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _resultsControl.fetchedObjects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID =@"contactcell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
//    sectionnum
//    0:在线
//    1:离开
//    2:离线
    
//    获取对应好友
    XMPPUserCoreDataStorageObject *friend =_resultsControl.fetchedObjects[indexPath.row];
    
    switch ([friend.sectionNum intValue]) {//好友状态
        case 0:
            cell.detailTextLabel.text = @"在线";
            break;
        case 1:
            cell.detailTextLabel.text = @"离开";
            break;
        case 2:
            cell.detailTextLabel.text = @"离线";
            break;
        default:
            break;
    }
      cell.textLabel.text = friend.jidStr;
     
    NSLog(@"获取好友");
 
    
    return cell;
    
    
}

#pragma mark - 删除好友
//cell往左滑就可以
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除好友");
        
        XMPPUserCoreDataStorageObject *friend = _resultsControl.fetchedObjects[indexPath.row];
        XMPPJID *friendJid = friend.jid;
        [[cCXMPPTool sharedcCXMPPTool].roster removeUser:friendJid];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    获取好友
     XMPPUserCoreDataStorageObject *friend =_resultsControl.fetchedObjects[indexPath.row];
//    选中表格进行聊天界面
    [self performSegueWithIdentifier:@"ChatSegue" sender:friend.jid];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id destVc = segue.destinationViewController;
    if ([destVc isKindOfClass:[cCChatViewController class]]) {
        cCChatViewController *chatVc = destVc;
        chatVc.friendJid = sender;
        
    }
}



@end
